﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinook.Facade;
using Chinook.Core.DaoInterface;
using Chinook.Core.DomainModel;
using Chinook.Facade.ViewModel;
using Chinook.Facade.Messaging;
using Chinook.Facade.Ioc;
using Base.Utils;
using Base.Utils.Logging;
using Base.Utils.Factory;

namespace Chinook.Facade
{
   public class EmployeeAppService : IEmployeeAppService
    {
       private IEmployeeDao _employeeDao;

       public IEmployeeDao EmployeeDao
       {
           get{
               if (_employeeDao != null)
                   return _employeeDao;
               _employeeDao = new FacadeExposer().EmployeeDao;
               return _employeeDao;
           }
       }

        #region IEmployeeAppService Members

       /// <summary>
       /// 
       /// </summary>
       /// <param name="employeeSearchRequest"></param>
       /// <returns></returns>
        public EmployeeSearchResponse SearchEmployee(Chinook.Facade.Messaging.EmployeeSearchRequest employeeSearchRequest)
        {
            EmployeeSearchResponse result = new EmployeeSearchResponse();
            try
            {
                List<Employee> employees = EmployeeDao.GetAll();
                List<EmployeeViewModel> employeeViewModels = new List<EmployeeViewModel>();
                int i = 0;
                for (i = 0; i < employees.Count; i++)
                {
                    EmployeeViewModel employeeViewModel = new EmployeeViewModel();
                    employeeViewModel.Address = employees[i].Address;
                    employeeViewModel.Fullname = employees[i].Name;
                    employeeViewModel.BirthDate = employees[i].BirthDate;
                    employeeViewModel.Email = employees[i].Email;
                    employeeViewModels.Add(employeeViewModel);
                }
                result.Success = true;

            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Message = ex.Message;
            }
            return result;           
        }

       /// <summary>
       /// 
       /// </summary>
       /// <param name="lookUpEmployeeRequest"></param>
       /// <returns></returns>
        public LookUpEmployeeResponse LookupEmployee(Chinook.Facade.Messaging.LookUpEmployeeRequest lookUpEmployeeRequest)
        {
            LookUpEmployeeResponse result = new LookUpEmployeeResponse();
            try
            {
                List<Employee> employees = EmployeeDao.GetAll();
                List<EmployeeViewModel> employeeViewModels = new List<EmployeeViewModel>();
                int i = 0;
                for (i = 0; i < employees.Count; i++)
                {
                    EmployeeViewModel employeeViewModel = new EmployeeViewModel();
                    employeeViewModel.Address = employees[i].Address;
                    employeeViewModel.Fullname = employees[i].Name;
                    employeeViewModel.BirthDate = employees[i].BirthDate;
                    employeeViewModel.Email = employees[i].Email;
                    employeeViewModels.Add(employeeViewModel);
                    //UtilsFactory.Logger.Log(employees[i].ToString(), LogType.Info);                    
                }
                result.EmployeeViewModel = employeeViewModels;
                result.Success = true;

            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Message = ex.Message;
            }
            return result;           
        }

        #endregion
    }
}
